package com.fengniu.ejb.dao.impl;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.log4j.Logger;
import org.jboss.ejb3.annotation.LocalBinding;
import org.jboss.ejb3.annotation.RemoteBinding;

import com.fengniu.ejb.dao.CouponDaoLocal;
import com.fengniu.ejb.dao.CouponDaoRemote;
import com.fengniu.ejb.dao.util.DaoUtil;
import com.fengniu.ejb.dao.util.IEJBConstants;
import com.fengniu.ejb.entity.CouponItem;

@Stateless
@LocalBinding(jndiBinding = IEJBConstants.CouponDaoLocalJndiName)
@RemoteBinding(jndiBinding = IEJBConstants.CouponDaoRemoteJndiName)
public class CouponDaoImpl implements CouponDaoLocal, CouponDaoRemote {
	/**
	 * 
	 */
	private static final long serialVersionUID = 3264534949922486678L;

	static Logger logger = Logger.getLogger(CouponDaoImpl.class);

	@PersistenceContext(unitName = "default")
	protected EntityManager em;

	public Long getCountCouponItem(long userSysId, long storeSysId, Integer[] store2CouponRelationIds,
			long couponSysId, Integer[] couponTypeIds, long couponIssueId) {
		Long result = new Long(0);
		Query q = this.getQueryStoreCouponItemBy(userSysId, storeSysId, store2CouponRelationIds, couponSysId,
				couponTypeIds, couponIssueId, false);

		result = (Long) q.getSingleResult();

		return result;
	}

	public List<CouponItem> getListCouponItem(long userSysId, long storeSysId, Integer[] store2CouponRelationIds,
			long couponSysId, Integer[] couponTypeIds, long couponIssueId, int pgno, int pgsize) {
		Query q = this.getQueryStoreCouponItemBy(userSysId, storeSysId, store2CouponRelationIds, couponSysId,
				couponTypeIds, couponIssueId, true);
		List result = DaoUtil.getResultList(q, pgno, pgsize);
		return result;
	}

	public CouponItem saveCouponItem(CouponItem couponItem) {
		return em.merge(couponItem);
	}

	public CouponItem findCouponItemByItemId(String couponItemId) {
		Query q = em.createNamedQuery("CouponDao.eql.findCouponItemByItemId")
				.setParameter("couponItemId", couponItemId).setMaxResults(1);
		List<CouponItem> couponItemList = q.getResultList();
		CouponItem result = null;
		if (couponItemList != null && couponItemList.size() > 0) {
			result = couponItemList.get(0);
		}
		return result;
	}

	private Query getQueryStoreCouponItemBy(long userSysId, long storeSysId, Integer[] store2CouponRelationIds,
			long couponSysId, Integer[] couponTypeIds, long couponIssueId, boolean isGetList) {

		String ent = isGetList ? "ci" : "count(ci)";

		String eql = "select " + ent + " from  CouponItem ci";
		String whereand = "";
		if (storeSysId > -1 || !DaoUtil.isNull(store2CouponRelationIds) || !DaoUtil.isNull(couponTypeIds)) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += " ,Site2Content s2c  ";
			eql += whereand + " ci.coupon.sysId = s2c.content.sysId";
		}

		if (storeSysId > -1) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  s2c.site.sysId=:storeSysId ";
		}
		if (DaoUtil.isNull(store2CouponRelationIds) == false) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  s2c.relationId IN (:store2CouponRelationIds) ";
		}
		if (couponSysId > -1) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  s2c.content.sysId=:couponSysId ";
		}
		if (DaoUtil.isNull(couponTypeIds) == false) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  s2c.content.typeId IN (:couponTypeIds) ";
		}

		if (couponIssueId > -1) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  ci.issueId=:couponIssueId ";
		}
		if (userSysId > -1) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  ci.user.sysId=:userSysId ";
		}
		if (isGetList) {
			eql += " ORDER BY ci.createdDate DESC";
		}
		logger.debug("eql=[" + eql + "]");

		Query q = em.createQuery(eql);
		if (storeSysId > -1) {
			q.setParameter("storeSysId", storeSysId);
		}
		if (DaoUtil.isNull(store2CouponRelationIds) == false) {
			List rts = DaoUtil.getListFromArray(store2CouponRelationIds);
			q.setParameter("store2CouponRelationIds", rts);
		}
		if (couponSysId > -1) {
			q.setParameter("couponSysId", couponSysId);
		}
		if (DaoUtil.isNull(couponTypeIds) == false) {
			List rts = DaoUtil.getListFromArray(couponTypeIds);
			q.setParameter("couponTypeIds", rts);
		}
		if (couponIssueId > -1) {
			q.setParameter("couponIssueId", couponIssueId);
		}
		if (userSysId > -1) {
			q.setParameter("userSysId", userSysId);
		}

		return q;
	}

	public CouponItem findCouponItemBySysId(long couponItemSysId) {
		return em.find(CouponItem.class, couponItemSysId);
	}

	public int updateCouponItemLotteryNumbers(CouponItem couponItem) {
		int result = 0;
		if (couponItem != null && couponItem.getSysId() != null && couponItem.getSysId() > 0) {
			Query q = em.createNamedQuery("CouponDao.eql.updateCouponItemLotteryNumbers")// ////////////////////////////////////////////
					.setParameter("lotteryNumber0", couponItem.getLotteryNumber0()) // ////////////////////////////////////////////
					.setParameter("lotteryNumber1", couponItem.getLotteryNumber1())// ////////////////////////////////////////////
					.setParameter("lotteryNumber2", couponItem.getLotteryNumber2())// ////////////////////////////////////////////
					.setParameter("lotteryNumber3", couponItem.getLotteryNumber3())// ////////////////////////////////////////////
					.setParameter("lotteryNumber4", couponItem.getLotteryNumber4())// ////////////////////////////////////////////
					.setParameter("lotteryNumber5", couponItem.getLotteryNumber5())// ////////////////////////////////////////////
					.setParameter("lotteryNumber6", couponItem.getLotteryNumber6())// ////////////////////////////////////////////
					.setParameter("lotteryNumber7", couponItem.getLotteryNumber7())// ////////////////////////////////////////////
					.setParameter("lotteryNumber8", couponItem.getLotteryNumber8())// ////////////////////////////////////////////
					.setParameter("lotteryNumber9", couponItem.getLotteryNumber9())// ////////////////////////////////////////////
					.setParameter("sysId", couponItem.getSysId());// ////////////////////////////////////////////

			// update record number
			result = q.executeUpdate();

		}
		return result;
	}

}
